<template>
	<view>
		<!-- 头部 -->
		<view class="main">
			<view class="userInfo" @click="Oninformation">
				<view class="userInfo_btn">
					<image mode="widthFix" @tap="preview_event" @error="user_avatar_error" :src="user_avatar || avatar">
					</image>
					<view class="franchisee" v-if="roleName!=null && roleName!=''">{{roleName}}</view>
				</view>
				<view class="userInfo-info">
					<view class="">{{nickname}}</view>
					<view class="phone">{{user.phone}}</view>
				</view>
			</view>
		</view>
		<!-- 申请加盟商  Apply for franchisee   v-if="roleName !== '加盟商'"  -->
		<view class="Apply_franchisee" @click="apply" v-if="vipCertification!= '1'">
			<image src="https://t1.chowtaiking.com.cn/img/ruoyi/2025/03/30/07718819f4b34abea5e74c329130618b.jpg"
				mode="widthFix"></image>
		</view>
		<!-- 我的订单 -->
		<view class="order">
			<view class="order-item">
				<view class="allOrder" @click="goMyOrder()">
					<view class="">全部订单</view>
					<image src="https://t1.chowtaiking.com.cn/img/ruoyi/2025/03/10/d0b60d4c410647ae8ede7a9a1ec7447a.png"
						mode="widthFix"></image>
				</view>
			</view>
			<view class="order-item">
				<view class="order-list" v-for="(item,index) in order_list" :key="index"
					@click="goMyOrder(item.status,isManager)">
					<image :src="item.images" mode="widthFix"></image>
					<view class="">{{item.name}}</view>
				</view>
			</view>
		</view>
		<!-- 我的应用 -->
		<view class="myapp">
			<view class="myapp-title">我的应用</view>
			<view class="myapp-item">
				<view class="item" @click="Oninformation()">
					<image src="https://t1.chowtaiking.com.cn/img/ruoyi/2025/03/10/b3ac5b0e9e8e426fb95e32fc1f7ad99e.png"
						mode="widthFix"></image>
					<view class="">个人信息</view>
				</view>
				<view class="item" @click="Onmy_tracks">
					<image src="https://t1.chowtaiking.com.cn/img/ruoyi/2025/03/10/38b46869c05f4dc2b664b0c5dcbb1ffe.png"
						mode="widthFix"></image>
					<view class="">我的足迹</view>
				</view>
				<view class="item" @click="Onmy_favorite">
					<image src="https://t1.chowtaiking.com.cn/img/ruoyi/2025/03/10/ff798022a2de4530b70d78c283075c19.png"
						mode="widthFix"></image>
					<view class="">我的收藏</view>
				</view>
				<view class="item" @click="OnAddress">
					<image src="https://www.moxikeji.com.cn/img/ruoyi/2025/02/20/d301b657cf874bb1b998225060505aea.png"
						mode="widthFix"></image>
					<view class="">地址管理</view>
				</view>
				<view class="item" @click="clear_cache_envent">
					<image src="https://t1.chowtaiking.com.cn/img/ruoyi/2025/03/10/dcd8fdf4d8cc44b5a94d3e594dc1c3d4.png"
						mode="widthFix"></image>
					<view class="">清缓存</view>
				</view>
			</view>
		</view>
		<!-- 加盟商管理 franchisee_modify -->
		<view class="franchisee_content" v-if="isManager===0 && admin_token !=''">
			<view class="franchisee_title">加盟商管理</view>
			<view class="franchisee_list">
				<!-- <view class="franchisee_item" @click="franchisee_modify_envent">
					<image src="https://t1.chowtaiking.com.cn/img/ruoyi/2025/03/13/2351783c2e8b47ad9abd442f715ed4cd.png"
						mode="widthFix"></image>
					<view class="">修改资料</view>
				</view> -->
				<view class="franchisee_item" @click="shop_add_envent">
					<image src="https://t1.chowtaiking.com.cn/img/ruoyi/2025/03/15/47f77f66f0d34293bf43ed9a99a1b907.png"
						mode="widthFix"></image>
					<view class="">添加门店</view>
				</view>
				<view class="franchisee_item" @click="shop_list_envent">
					<image src="https://t1.chowtaiking.com.cn/img/ruoyi/2025/03/15/b38924b414cf4cd19a57d10e1552a256.png"
						mode="widthFix"></image>
					<view class="">门店列表</view>
				</view>
				<view class="franchisee_item" @click="clerk_add_envent">
					<image src="https://t1.chowtaiking.com.cn/img/ruoyi/2025/03/13/33d8daf5dfa64f7192fd5528ba28ec2c.png"
						mode="widthFix"></image>
					<view class="">添加店员</view>
				</view>
				<view class="franchisee_item" @click="clerk_list_envent">
					<image src="https://t1.chowtaiking.com.cn/img/ruoyi/2025/03/14/a8b1217d576b45b2bc4fbe544b5cd317.png"
						mode="widthFix"></image>
					<view class="">店员列表</view>
				</view>
			</view>
		</view>
		<!-- 业务经理 business_manager  -->
		<view class="business_manager" v-if="isRecommend === 1 && admin_token !=''">
			<view class="business_manager_title">业务经理管理</view>
			<view class="business_manager_list">
				<!-- <view class="business_manager_item" @click="onmodify">
					<image src="https://t1.chowtaiking.com.cn/img/ruoyi/2025/03/10/9edd799604a147f6b526082a6d70200c.png"
						mode="widthFix"></image>
					<view class="">修改资料</view>
				</view> -->
				<view class="business_manager_item" @click="business_list_envent">
					<image src="https://t1.chowtaiking.com.cn/img/ruoyi/2025/03/16/1a0e680048dc437a9ec2b976e572469e.png"
						mode="widthFix"></image>
					<view class="">加盟商</view>
				</view>
			</view>
		</view>
		<!-- 底部 -->
		<view class="footer">
			<view class="footer_title">MoXi墨曦珠宝科技</view>
			<view class="footer_tip">提供技术支持</view>
		</view>
	</view>
</template>

<script>
	const app = getApp();
	var c_static_urls = app.globalData.get_static_urls();

	export default {
		data() {
			return {
				goodsList: [],
				nickname: "用户名",
				admin_token: "",
				avatar: app.globalData.data.default_user_head_src,
				user_avatar: "",
				anavigation: [],
				c_static_urls: c_static_urls,
				user: {},
				order_list: [
					{
						images: "https://t1.chowtaiking.com.cn/img/ruoyi/2025/03/23/7fc7ac80369a4267b59609067806e5a1.png",
						name: "待确认",
						status: 8
					},
					{
						images: "https://t1.chowtaiking.com.cn/img/ruoyi/2025/03/10/37af1f9a35e343c882aa2b044d577480.png",
						name: "已接单",
						status: 9
					},
					{
						images: "https://t1.chowtaiking.com.cn/img/ruoyi/2025/04/11/1235841abcb44599aae4c0929a0ae306.png",
						name: "已完成",
						status: 13
					},
				],
				entryTime: 0,
				integral: '',
				vipCertification: 0,
				// 切换后台
				system_back: null,
				backX: 0,
				backY: 0,
				backTop: 0,
				height_dec_back: 0,
				isManager: null, // 是否加盟商管理
				isOrders: null, // 是否能够下单
				isRecommend: null, //是否业务经理，
				roleName: null, //角色名字
			}
		},
		created() {
			var system = app.globalData.get_system_info(null, null, true);
			var width = app.globalData.window_width_handle(system.windowWidth);
			var height = app.globalData.window_height_handle(system);
			// 切换后台
			this.system_back = system;
			this.backX = width - 43;
			this.backY = height - 150;
			this.backTop = 0;
			this.height_dec_back = 0;
		},
		methods: {
			// 初始化
			init() {
				this.user = app.globalData.get_user_info(this, "init", "");
				if (this.user != false) {
					// 用户未绑定用户则转到登录页面
					if (app.globalData.user_is_need_login(this.user)) {
						uni.navigateTo({
							url: "/pages/login/login?event_callback=init"
						})

					} else {
						this.vipCertification = this.user.vipCertification;
						this.roleName = this.user.roleName;
						this.get_data();
					}
				}
			},
			OnEntryTime() {
				var radarlog_data = {
					pageUrl: "pages/user/user",
					name: "我的页面",
					remark: "我的页面停留时间",
					entryTime: this.entryTime,
				}
				uni.request({
					url: app.globalData.get_request_url("radarlog/add"),
					data: radarlog_data,
					method: "POST",
					success: (res) => {
						console.log(res, "我的页面停留时间");
					}
				})
			},
			get_data() {
				var url = app.globalData.get_request_url("center");
				uni.request({
					url: url,
					method: 'POST',
					data: {},
					dataType: 'json',
					header: {
						'Authorization': 'Bearer ' + app.globalData.data.admin_token
					},
					success: (res) => {
						if (res.data.code == 200) {
							var data = res.data.data;
							this.integral = data.members.integral;
							this.isManager = data.isManager; // 是否加盟商管理  0是  1不是
							this.isOrders = data.isOrders; // 是否能够下单
							this.isRecommend = data.isRecommend; //是否业务经理 1 是 0 不是
							
							app.globalData.data.isManager = data.isManager;
							app.globalData.data.isRecommend = data.isRecommend;
							// 导航购物车处理
							var cart_total = data.cart_total.buy_number || 0;
							// if (cart_total <= 0) {
							// 	uni.removeTabBarBadge({
							// 		index: 3
							// 	});
							// } else {
							// 	uni.setTabBarBadge({
							// 		index: 3,
							// 		text: String(cart_total)
							// 	})
							// }
							this.user_avatar = data.avatar;
							this.nickname = data.nickname;
							if (this.user_avatar == '/default-user-avatar.gif') {
								this.user_avatar = app.globalData.data.default_user_head_src;
							}
							this.admin_token = data.token;
							app.globalData.data.admin_token = data.token;
							this.anavigation = data.anavigation;
						} else {
							if (app.globalData.is_login_check(res.data, this, 'get_data')) {
								uni.showToast({
									icon: 'none',
									title: res.data.msg,
									duration: 3000
								});
							}
						}
					}
				})
			},
			// 跳到联系导购页面
			goScan() {
				uni.navigateTo({
					url: "/pages/user_card/user_card"
				})
			},
			// 跳到修改个人信息页面
			Oninformation() {
				uni.navigateTo({
					url: "/pages/user_setInfo/setInfo"
				})
			},
			// 跳转申请域名页面
			apply() {
				uni.navigateTo({
					url: '/pages/apply/apply'
				})
			},
			// 跳转到我的订单
			goMyOrder(status,isManager) {
				uni.navigateTo({
					url: "/pages/user-order/user-order?status=" + status + '&isManager=' + this.isManager
				})
			},
			// 我的足迹
			Onmy_tracks() {
				uni.navigateTo({
					url: "/pages/my_tracks/my_tracks"
				})
			},
			// 我的收藏
			Onmy_favorite() {
				uni.navigateTo({
					url: "/pages/my_favorite/my_favorite"
				})
			},
			// 地址管理
			OnAddress() {
				uni.navigateTo({
					url: "/pages/user-address/user-address"
				})
			},
			// 清除缓存
			clear_cache_envent() {
				uni.showModal({
					title: '温馨提示',
					content: '确定要清除缓存吗?',
					cancelText: "取消",
					confirmText: "确定",
					success: (result) => {
						if (result.confirm) {
							uni.showLoading({
								title: '清除中...'
							});
							setTimeout(function() {
								uni.clearStorageSync();
								uni.showToast({
									title: "清除成功"
								});
								uni.switchTab({
									url: "/pages/index/index"
								});
							}, 3000);
						}
					}
				})
			},
			// 头像加载错误
			user_avatar_error(e) {
				this.avatar = app.globalData.data.default_user_head_src
			},
			// 头像查看
			preview_event() {
				if (app.globalData.data.default_user_head_src != this.avatar) {
					uni.previewImage({
						current: this.avatar,
						urls: [this.avatar]
					});
				}
			},
			// 跳转雷达
			Onradar() {
				uni.navigateTo({
					url: "/pages/user_radar/user_radar"
				})
			},
			// 修改资料   加盟商
			franchisee_modify_envent() {
				uni.navigateTo({
					url: "/pages/franchisee_modify/franchisee_modify"
				})
			},
			// 添加门店
			shop_add_envent() {
				uni.navigateTo({
					url: '/pages/shop_add/shop_add'
				})
			},
			// 门店列表
			shop_list_envent() {
				uni.navigateTo({
					url: '/pages/shop_list/shop_list'
				})
			},
			// 跳转修改名片
			onmodify() {
				uni.navigateTo({
					url: "/pages/card_modify/card_modify"
				})
			},
			// 添加店员
			clerk_add_envent() {
				uni.navigateTo({
					url: '/pages/clerk_add/clerk_add'
				})
			},
			// 店员列表
			clerk_list_envent() {
				uni.navigateTo({
					url: "/pages/clerk_list/clerk_list"
				})
			},
			// 加盟商审核管理
			business_examine_envent() {
				uni.navigateTo({
					url: '/pages/business_examine/business_examine'
				})
			},
			// 加盟商列表
			business_list_envent() {
				uni.navigateTo({
					url: '/pages/business_list/business_list'
				})
			}
		},
		// 分享给朋友
		onShareAppMessage(res) {
			return {
				title: app.globalData.data.tenantName,
				path: "pages/user/user?numberId=" + app.globalData.data.numberId,
				mpId: app.globalData.data.appid
			}
		},
		// 分享朋友圈
		// onShareTimeline(res) {
		// 	return {
		// 		title: app.globalData.data.tenantName,
		// 		type: 0,
		// 		summary: ""
		// 	}
		// },
		onShow() {
			this.init();
			// 记录用户进来时间
			this.entryTime = Date.now();
		},
		onHide() {
			this.OnEntryTime();
		},
		onLoad(params) {
			if (typeof(params) != 'undefined') {
				var numberId = params.numberId || '';
				if (numberId != '') {
					uni.clearStorageSync();
					app.globalData.data.numberId = numberId;
				}
			}
		},
		onPullDownRefresh() {
			var that = this;
			setTimeout(function() {
				uni.stopPullDownRefresh();
				that.init();
			}, 1000);
		}
	}
</script>

<style>
	@import url("user.css");
</style>